<template>
  <div class="collectList">
    <PageTitle title="收藏列表"></PageTitle>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh" class="content">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="到底了"
        @load="loadMore"
      >
        <div class="wrap">
          <van-swipe-cell v-for="item in collectList" :key="item.id">
            <van-card
              class="goods-card"
              :price="item.price"
              :origin-price="item.ot_price"
              :title="item.store_name"
              :thumb="item.image"
              @click="$router.push('/detail/' + item.pid)"
            />
            <template #right>
              <van-button
                square
                type="info"
                class="delete-button"
                @click="delCollect(item.pid)"
              >
                移除收藏
              </van-button>
            </template>
          </van-swipe-cell>
        </div>
        <van-empty v-if="collectList && collectList.length == 0" />
      </van-list>
    </van-pull-refresh>
    <div class="wrap"></div>
  </div>
</template>

<script>
import PageTitle from "@/components/PageTitle";
import { mapMutations } from "vuex";
export default {
  name: "CollectList",
  data() {
    return {
      collectList: null,
      page: 1,
      limit: 8,
      loading: false,
      finished: false,
      refreshing: false,
      getting: false,
    };
  },
  components: { PageTitle },
  activated() {
    this.getCollectList();
  },
  methods: {
    ...mapMutations(["changeLoading"]),
    getCollectList(loadMore) {
      this.getting = true;
      if (loadMore) {
        this.page++;
      } else {
        this.page = 1;
        this.finished = false;
      }
      this.$axios
        .get("/api/collect/user", {
          params: {
            page: this.page,
            limit: this.limit,
          },
        })
        .then((d) => {
          this.getting = false;
          console.log("collect", this.page, d.data);
          if (this.refreshing) this.refreshing = false;
          if (this.loading) this.loading = false;

          if (d.data.status == 200) {
            if (loadMore) {
              this.collectList = this.collectList.concat(d.data.data);
            } else {
              this.collectList = d.data.data;
            }
            if (d.data.data.length == 0) this.finished = true;
          } else if (d.data.status == 410000) {
            this.$toast.fail("请重新登录");
            this.$store.commit("loginOut");
            this.$router.push("/login");
          }
        });
    },
    loadMore() {
      if (this.getting) return;
      if (!this.collectList) return;
      this.getCollectList(true);
    },
    onRefresh() {
      this.loading = true;
      this.getCollectList();
    },
    delCollect(id) {
      this.changeLoading(true);
      this.$axios
        .post("/api/collect/del", {
          category: "product",
          id,
        })
        .then((d) => {
          console.log("collect/del", d.data);
          this.changeLoading(false);
          if (d.data.status == 200) {
            this.collectList = this.collectList.filter(
              (item) => item.pid != id
            );
            this.$toast.success("移除成功");
          } else if (d.data.status == 410000) {
            this.$toast.fail("请重新登录");
            this.$store.commit("loginOut");
            this.$router.push("/login");
          }
        });
    },
  },
};
</script>

<style lang="less" scoped>
.collectList {
  padding-top: 56px;
  .pageTitle {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 999;
  }
  .content {
    min-height: calc(100vh - 56px);
    padding-top: 5px;
    .wrap {
      .van-swipe-cell {
        .goods-card {
          padding: 15px 25px;
          margin-top: 10px;
          background: #fff;
          border-radius: 5px;
          .van-card__thumb {
            width: 70px;
            height: 70px;
          }
          .van-card__content {
            min-height: 70px;
          }
        }
        .delete-button {
          height: 100%;
          background: #25c9c3;
          border: none;
          padding: 0 15px;
          border-radius: 0 10px 10px 0;
          font-size: 13px;
          width: 13px;
          box-sizing: content-box;
        }
      }
    }
  }
}
</style>